package com.sam.controller;

import java.util.Calendar;
import java.util.Date;
//import java.util.Date;
import java.util.GregorianCalendar;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

public class ThreatController
{

	String method;
	int mins = 0;
	int ij = 0;

	ThreatController(int ID)
	{

		System.loadLibrary("opencv_java244");
		if (ID == 1)
			method = "FaceDetection";
		else if (ID == 2)
			method = "MotionDetection";
		else if (ID == 3)
			method = "TheftDetection";
	}

	String getmethod()
	{
		return method;
	}

	void setmethod(String me)
	{
		method = me;
	}

	boolean FaceDetection(Mat frame)
	{
		CascadeClassifier faceDetector = new CascadeClassifier(
				"haarcascade_frontalface_default.xml");
		CascadeClassifier eyeDetector = new CascadeClassifier(
				"frontalEyes35x16.xml");
		// Detect faces in the image.
		// MatOfRect is a special container class for Rect.
		MatOfRect faceDetections = new MatOfRect();
		faceDetector.detectMultiScale(frame, faceDetections);
		MatOfRect eyeDetections = new MatOfRect();
		eyeDetector.detectMultiScale(frame, eyeDetections);

		// System.out.println(String.format("Detected %s faces",
		// faceDetections.toArray().length));
		// System.out.println(String.format("Detected %s eyes",
		// eyeDetections.toArray().length));

		// Draw a bounding box around each face.
		if (faceDetections.toArray().length > 0
				&& eyeDetections.toArray().length > 0)

			for (Rect rect : faceDetections.toArray())
			{
				for (Rect rect2 : eyeDetections.toArray())
				{

					if ((rect.x < rect2.x && rect.y < rect2.y)
							&& (rect.x + rect.width > rect2.x + rect2.width && rect.y
									+ rect.height > rect2.y + rect2.height))
					{
						Core.rectangle(frame, new Point(rect.x, rect.y),
								new Point(rect.x + rect.width, rect.y
										+ rect.height), new Scalar(0, 255, 0),
								0);
						Core.rectangle(frame, new Point(rect2.x, rect2.y),
								new Point(rect2.x + rect2.width, rect2.y
										+ rect2.height), new Scalar(0, 255, 0),
								0);

						Highgui.imwrite("frame.jpg", frame);

						return true;
					}

				}
			}
		Highgui.imwrite("frame.jpg", frame);
		return false;
	}// end FaceDetection()

	public boolean MotionDetection(Mat frame, Mat firstFrame, Mat midframe)
	{
		System.out.println("MotionDetection");
		Mat result = new Mat();
		Mat result2 = new Mat();
		Mat diff = new Mat();

		int red = 0;
		int green = 0;
		int blue = 0;

		boolean faceDetectionFlag = FaceDetection(frame);
		if (faceDetectionFlag == true)
		{
			System.out.println("Face Detected by SaM");
			return true;
		}

		// Motion detection Algorithm
		Highgui.imwrite("frameIn" + ij + ".jpg", frame);
		Highgui.imwrite("firstFrameIn" + ij + ".jpg", firstFrame);
		Highgui.imwrite("midframeIn" + ij + ".jpg", midframe);
		Core.absdiff(frame, midframe, result);
		Core.absdiff(midframe, firstFrame, result2);
		Highgui.imwrite("mcresult" + ij + ".jpg", result);
		Highgui.imwrite("mfresult" + ij + ".jpg", result2);
		Core.bitwise_xor(result, result2, diff);
		Highgui.imwrite("beforeTh.jpg", diff);
		Imgproc.threshold(diff, diff, 55, 255, Imgproc.THRESH_BINARY);
		Highgui.imwrite("diff" + ij + ".jpg", diff);

		double[] a = new double[3];
		for (int i = 0; i < diff.width() - 1; i++)
			for (int j = 0; j < diff.height() - 1; j++)
			{

				if (diff.get(j, i)[0] == 0 && diff.get(j, i)[1] == 0
						&& diff.get(j, i)[2] == 0)
				{
					frame.put(j, i, diff.get(j, i));
				}

			}

		// human body detection
		// Highgui.imwrite("diff3"+ij+".jpg", diff);
		// thresholding based on RGB values
		for (int i = 0; i < diff.height(); i++)
			for (int k = 0; k < diff.width(); k++)
			{
				double[] ab = diff.get(i, k);
				if (ab[0] > 0)
				{
					System.out.println("a[0]= " + ab[0]);
					red++;
				}
				if (ab[1] > 0)
				{
					System.out.print("a[1]= " + ab[1]);
					blue++;
				}
				if (ab[2] > 0)
				{
					System.out.println("a[2]= " + ab[2]);
					green++;
				}
			}

		System.out.println("color red =" + red);
		System.out.println("color blue =" + blue);
		System.out.println("color green =" + green);

		if (red > 25000 || blue > 25000 || green > 25000)
		{
			Highgui.imwrite("finalresult" + ij + ".jpg", frame);

			// Attempt to detect face after motion detection
			faceDetectionFlag = FaceDetection(frame);
			if (faceDetectionFlag == true)
			{
				System.out.println("Face Detected with motion by SaM");
				return true;
			}
			else
			{
				// Attempt to detect human body (Upper body)
				CascadeClassifier bodyDetector = new CascadeClassifier(
						"haarcascade_upperbody.xml");

				MatOfRect bodyDetections = new MatOfRect();
				bodyDetector.detectMultiScale(frame, bodyDetections);
				if (bodyDetections.toArray().length > 0)
				{
					for (Rect rect3 : bodyDetections.toArray())
					{
						Core.rectangle(frame, new Point(rect3.x, rect3.y),
								new Point(rect3.x + rect3.width, rect3.y
										+ rect3.height), new Scalar(0, 255, 0),
								0);
						Highgui.imwrite("frame.jpg", frame);
						System.out
								.println("Motion detected with/without Human body");
						return true;
					}
				}
			}
		}
		Highgui.imwrite("frame.jpg", frame);

		return false;
	}

}
